package control;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.board.BoardVO;
import model.gallery.GalleryService;
import model.gallery.GalleryVO;
import model.member.MemberVO;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class GalleryController extends MultiActionController {
	private GalleryService galleryService;

	public GalleryController(GalleryService galleryService) {
		super();
		this.galleryService = galleryService;
	}

	public ModelAndView write(HttpServletRequest request,
			HttpServletResponse response, GalleryVO vo) throws Exception {
		ModelAndView mv = new ModelAndView();
		
		String path = "index";
		try {
			HttpSession session = request.getSession(false);
			if(session != null && session.getAttribute("mvo")!=null){
				MemberVO memberVO = (MemberVO) session.getAttribute("mvo");
				vo.setMemberVO(memberVO);
				galleryService.write(vo);
				path = "redirect:gallery.do?mode=content&no="+vo.getNo();
				
			}
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}	
		mv.setViewName(path);
		return mv;
	}

	public ModelAndView content(HttpServletRequest request,
			HttpServletResponse response, GalleryVO vo) throws SQLException {
		GalleryVO gvo = null;
		try {
			gvo = galleryService.content(vo.getNo());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("content.gallery","gvo",gvo);		
	}
}
